package cn.enilu.flash.api.controller.h5;

import cn.enilu.flash.bean.entity.system.Cfg;
import cn.enilu.flash.bean.entity.system.User;
import cn.enilu.flash.bean.enumeration.ConfigKeyEnum;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.task.MyOrderVo;
import cn.enilu.flash.bean.vo.task.MyTaskVo;
import cn.enilu.flash.bean.vo.task.MyVo;
import cn.enilu.flash.security.JwtUtil;
import cn.enilu.flash.service.order.TaskOrderService;
import cn.enilu.flash.service.system.CfgService;
import cn.enilu.flash.service.system.UserService;
import cn.enilu.flash.service.task.HelpTaskService;
import cn.enilu.flash.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/h5")
public class MyController {
    @Autowired
    private UserService userService;
    @Autowired
    private TaskOrderService taskOrderService;
    @Autowired
    private HelpTaskService helpTaskService;
    @Autowired
    private CfgService cfgService;

    @GetMapping("/getMyDate")
    public Ret getMyDate(){
        User user = userService.get(JwtUtil.getUserId());
        if(user==null) return Rets.failure("用户不存在~");
        MyVo myVo = new MyVo();
        BeanUtils.copyProperties(user,myVo);
        if(StringUtil.isNotEmpty(user.getAvatar())){
            myVo.setAvatarIdFile(Integer.valueOf(user.getAvatar()));//头像
        }else {
            myVo.setAvatarIdFile(null);
        }
        List<SearchFilter> myTaskList = new ArrayList<>();
        List<SearchFilter> myTaskOrderList = new ArrayList<>();
        myTaskList.add(new SearchFilter("createBy", SearchFilter.Operator.EQ,user.getId()));
        myTaskList.add(new SearchFilter("state", SearchFilter.Operator.EQ,0));// 待支付
        MyTaskVo myTask = new MyTaskVo();
        myTask.setWaitPayNum(helpTaskService.count(myTaskList));

        myTaskList.remove(1);
        myTaskList.add(new SearchFilter("state", SearchFilter.Operator.EQ,2));// 审核中
        myTask.setAuditingNum(helpTaskService.count(myTaskList));

        myTaskList.remove(1);
        myTaskList.add(new SearchFilter("state", SearchFilter.Operator.EQ,3));// 待修改
        myTask.setWaitModifyNum(helpTaskService.count(myTaskList));

        myTaskOrderList.add(new SearchFilter("issuerId", SearchFilter.Operator.EQ,user.getId()));
        myTaskOrderList.add(new SearchFilter("state", SearchFilter.Operator.EQ,5));// 申诉中
        myTask.setAppealingNum(taskOrderService.count(myTaskOrderList));
        myTaskOrderList.remove(1);
        myTaskOrderList.add(new SearchFilter("state", SearchFilter.Operator.EQ,2));// 审核订单
        myTask.setAuditOrderNum(taskOrderService.count(myTaskOrderList));


        /***************************************我的订单********************************************/
        MyOrderVo myOrder = new MyOrderVo();
        myTaskOrderList = new ArrayList<>();
        myTaskOrderList.add(new SearchFilter("createBy", SearchFilter.Operator.EQ,user.getId()));
        myTaskOrderList.add(new SearchFilter("state", SearchFilter.Operator.EQ,1));// 待提交
        myOrder.setWaitSubmitNum(taskOrderService.count(myTaskOrderList));

        myTaskOrderList.remove(1);
        myTaskOrderList.add(new SearchFilter("state", SearchFilter.Operator.EQ,5));// 申诉中
        myOrder.setAppealingNum(taskOrderService.count(myTaskOrderList));

        myTaskOrderList.remove(1);
        myTaskOrderList.add(new SearchFilter("state", SearchFilter.Operator.EQ,2));// 审核中
        myOrder.setAuditingNum(taskOrderService.count(myTaskOrderList));
        myVo.setMyTask(myTask);
        myVo.setMyOrder(myOrder);
        String monthVipPrice = cfgService.get(new SearchFilter("cfgName", SearchFilter.Operator.EQ, "monthVip")).getCfgValue();
        String seasonVipPrice = cfgService.get(new SearchFilter("cfgName", SearchFilter.Operator.EQ, "seasonVip")).getCfgValue();
        String yearVipPrice = cfgService.get(new SearchFilter("cfgName", SearchFilter.Operator.EQ, "yearVip")).getCfgValue();
        myVo.setMonthVipPrice(new BigDecimal(monthVipPrice));
        myVo.setSeasonVipPrice(new BigDecimal(seasonVipPrice));
        myVo.setYearVipPrice(new BigDecimal(yearVipPrice));
        Cfg VIP_SERVICE_CHARGE = cfgService.get(SearchFilter.build("cfgName", ConfigKeyEnum.VIP_SERVICE_CHARGE.getValue()));
        Cfg SERVICE_CHARGE = cfgService.get(SearchFilter.build("cfgName", ConfigKeyEnum.SERVICE_CHARGE.getValue()));
        Double v = Double.valueOf(VIP_SERVICE_CHARGE.getCfgValue()) * 100;
        String vipRate = v.intValue()+"%";
        Double v1 = Double.valueOf(SERVICE_CHARGE.getCfgValue()) * 100;
        String rate = +v1.intValue()+"%";
        myVo.setVipRate(vipRate);
        myVo.setRate(rate);
        return Rets.success(myVo);
    }
}
